package it.gameplanet.client;

import it.gameplanet.client.widgets.GamePlanetTree;
import it.gameplanet.model.User;
import it.gameplanet.service.GamePlanetService;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class GamePlanetContext {

	private static Widget homePagePanel;
	private static GamePlanetTree articoliTree = new GamePlanetTree();
	private static User user;
	
	public static User getUser() {
		return user;
	}

	public static GamePlanetTree getArticoliTree() {
		return articoliTree;
	}

	public static void resetHomePage() {
		
		GamePlanetContext.setContentPanel(homePagePanel);
	}

	public static boolean isAdminUser() {
		return GamePlanetContext.getUser()!=null && GamePlanetContext.getUser().isAdministrator();
	}
	
	public static void bootstrap(final AsyncCallback<User> callback) {
		GamePlanetService.Utils.getGamePlanetServiceAsync().getCurrentUser(new AsyncCallback<User>() {

			@Override
			public void onFailure(Throwable caught) {
				callback.onFailure(caught);
			}

			@Override
			public void onSuccess(User userU) {
				GamePlanetContext.user = userU;
				callback.onSuccess(user);
			}
		});
		
	}
	
	public static void setContentPanel(Widget w) {
		RootPanel.get("contentContainer").clear();
		RootPanel.get("contentContainer").add(w);

	}

	public static void setHomePage(Widget homePagePanel) {
		GamePlanetContext.homePagePanel = homePagePanel;
		setContentPanel(homePagePanel);
	}
}
